package com.liang.jiekou.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liang.jiekou.entity.COPTC;
import com.liang.jiekou.entity.COPTD;
import com.liang.jiekou.entity.result.COPTCD;
import com.liang.jiekou.mapper.COPTCDMapper;
import com.liang.jiekou.service.COPTCDService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class COPTCDServiceImpl implements COPTCDService {
    @Resource
    private COPTCDMapper coptcdMapper;


    @Override
    public COPTCD selectCOPTCD(String TC001, String TC002) {
        COPTC coptc = coptcdMapper.selectCOPTC(TC001, TC002);
        List<COPTD> coptdList = coptcdMapper.selectCOPTD(TC001, TC002);


        COPTCD coptcd = new COPTCD();
        coptcd.setCoptc(coptc);
        coptcd.setCoptdList(coptdList);

        return coptcd;
    }


    @Override
    public IPage<COPTCD> selectCOPTCDall(Page<COPTCD> page,String startcreate, String endcreate, String startupdate, String endupdate) {
        IPage<COPTC> coptcPage = coptcdMapper.selectCOPTCall(page,startcreate,endcreate,startupdate,endupdate);
        List<COPTC> coptcList = coptcPage.getRecords();

        IPage<COPTD> coptdPage = coptcdMapper.selectCOPTDall(page,startcreate,endcreate,startupdate,endupdate);
        List<COPTD> coptdList = coptdPage.getRecords();

        List<COPTCD> coptcdList = new ArrayList<>();
        for (COPTC coptc : coptcList) {
            COPTCD coptcd = new COPTCD();
            coptcd.setCoptc(coptc);
            List<COPTD> matchedCoptdList = new ArrayList<>();
            for (COPTD coptd : coptdList) {
                if (coptc.getTC001().equals(coptd.getTD001()) && coptc.getTC002().equals(coptd.getTD002())) {
                    matchedCoptdList.add(coptd);
                }
            }
            coptcd.setCoptdList(matchedCoptdList);
            coptcdList.add(coptcd);
        }

        IPage<COPTCD> resultPage = new Page<>(page.getCurrent(), page.getSize(), coptcPage.getTotal());
        resultPage.setRecords(coptcdList);

        return resultPage;
    }

    @Override
    public Page<COPTCD> selectCoptcdPage(Integer page, Integer pageSize, String startcreate, String endcreate, String startupdate, String endupdate) {

        List<COPTC> coptcListAll =
                coptcdMapper.selectCoptcListByCreateDateWithModiDate(startcreate,endcreate,startupdate,endupdate);
        List<COPTCD> coptcdList = new ArrayList<>();
        int total = coptcListAll.size();
        int start = (page - 1 ) * pageSize;
        int end  = page * pageSize;
        //if(end > total) end = total;
        if (end <= total){
            for (int i = start; i < end; i++) {
                COPTCD coptcd = new COPTCD();
                coptcd.setCoptc(coptcListAll.get(i));
                List<COPTD> coptdList = coptcdMapper.selectCoptdList(coptcListAll.get(i).getTC001(),coptcListAll.get(i).getTC002());
                coptcd.setCoptdList(coptdList);
                coptcdList.add(coptcd);
            }
        }
        else if(end > total && start < end ){
            for (int i = start; i < total; i++) {
                COPTCD coptcd = new COPTCD();
                coptcd.setCoptc(coptcListAll.get(i));
                List<COPTD> coptdList = coptcdMapper.selectCoptdList(coptcListAll.get(i).getTC001(),coptcListAll.get(i).getTC002());
                coptcd.setCoptdList(coptdList);
                coptcdList.add(coptcd);
            }
        }



        Page<COPTCD> coptcdPage = new Page<>(page,pageSize);
        coptcdPage.setRecords(coptcdList);
        coptcdPage.setTotal(coptcListAll.size());
        return coptcdPage;
    }
}
